package com.core.pf;

import java.util.List;
import java.util.ArrayList;

import org.hibernate.criterion.Expression;
import org.hibernate.criterion.SimpleExpression;

/**
 * 
 * @author jorge
 *
 */
public class RetrieveCriteria {
	
	// Constantes
	public static final int ASCENDING = 1;
	public static final int DESCENDING = 2;

    // Privados
    @SuppressWarnings("unchecked")
	private Class clase;
    private Object pkObj;
    @SuppressWarnings("unchecked")
	private List conditions;
    @SuppressWarnings("unchecked")
    private List orders;
    
   /**
    * Constructor
    * Obtengo el criterio de busqueda 
    */
    @SuppressWarnings("unchecked")
	public RetrieveCriteria(Class objClass) {
        super();
        this.clase = objClass;
        this.conditions = new ArrayList();
    }
    /**
     * Para obtener el objeto por Pk
     * @param pkObj
     */
    public void addPkToCriteria(Object pkObj) {
        this.pkObj = pkObj;
    }
    /**
     * Para obtener el objeto por criterio
     * @param attribute
     * @param value
     */
    @SuppressWarnings("unchecked")
	public void addEqualToCriteria(String attribute, Object value) {
        SimpleExpression expresion = Expression.eq(attribute, value);
        this.conditions.add(expresion);
    }
    
    /**
     * 
     * @param attribute
     * @param value
     */
    @SuppressWarnings("unchecked")
	public void gratherOrEqualThan(String attribute, Object value) {
        SimpleExpression expresion = Expression.ge(attribute, value);
        this.conditions.add(expresion);   	
    }
    
    /**
     * 
     * @param attribute
     * @param value
     */
	@SuppressWarnings("unchecked")
	public void lessOrEqualThan(String attribute, Object value) {
        SimpleExpression expresion = Expression.le(attribute, value);
        this.conditions.add(expresion);   	
    }
	
	/**
	 * 
	 * @param attribute
	 * @param value
	 */
	@SuppressWarnings("unchecked")
	public void addNotEqualToCriteria(String attribute, Object value) {
        SimpleExpression expresion = Expression.ne(attribute, value);
        this.conditions.add(expresion);
    }
	
	/**
	 * 
	 * @param attribute
	 * @param value
	 */	
	@SuppressWarnings("unchecked")
	public void addLikeToCriteria(String attribute, Object value) {
        SimpleExpression expresion = Expression.like(attribute, value);
        this.conditions.add(expresion);
	}
    
    // Getter
    @SuppressWarnings("unchecked")
	public Class getClase() {
        return clase;
    }
    public Object getPkObj() {
        return pkObj;
    }
    @SuppressWarnings("unchecked")
	public List getConditions() {
        return conditions;
    }
    
    public void addOrder(String field, int ascOrDsc){
    	Order order = new Order(field, ascOrDsc);
    	getOrders().add(order);
    }
    
    /**
     * 
     * @return
     */
    public List getOrders() {
    	if(orders == null)
    		orders = new ArrayList();
    	
		return orders;
	}

	/**
     * Clase para manejar los ordenes.
     * 
     * @author jorge
     *
     */
    class Order {
    	private String field;
    	private int typeOrder;
    	
    	public Order(String field, int type) {
    		this.field = field;
    		this.typeOrder = type;
    	}

		public String getField() {
			return field;
		}

		public int getTypeOrder() {
			return typeOrder;
		}    	
    }
    
}
